<template>
	<view>
		<view style="height: 160rpx;box-sizing:border-box; padding-top: 81rpx;border-bottom: 2rpx solid #F7F7F7;display: flex;">
			<image src="/static/shequ/fh.png" @click="backTo()" style="margin: 0 259rpx 0 30rpx; width: 18rpx;height: 32rpx;" mode="aspectFit"></image>
			<text style="font-size: 32rpx;">收货地址</text>
		</view>
		<view>
			<block v-for="(item, index) in addressList" :key="index">
				<view class="address" @click="chose(item)">
					<view class="it1">
						<text style="font-family: Source Han Sans CN;color: #1B1B1B;">{{item.realname}}</text>
						<text style="font-family: PingFang SC;color: #071228;margin-left: 24rpx;">{{item.mobile}}</text>
					</view>
					<view class="it2">
						<text>{{item.address}}</text>
					</view>
					<view class="it3">
						<checkbox-group>
							<label>
								<checkbox value="cb"
									style="font-size: 26rpx;font-family: Source Han Sans CN;font-weight: 400;color: #19110B;">
									设为默认</checkbox>
							</label>
						</checkbox-group>
						<view class="btn" @click.stop="edit(item)">
							<image src="@/static/bianji.png" mode="aspectFill" style="margin-left: 224rpx;"></image>
							<text>编辑</text>
						</view>
						<view class="btn" @click.stop="deleteBtn(index,item.id)">
							<image src="@/static/delete.png" mode="aspectFill" style="margin-left: 40rpx;"></image>
							<text>删除</text>
						</view>
					</view>
				</view>
			</block>
		</view>
		<view class="footer" @click.stop="goto('/pages/add/add')">
			<button type="button">添加新地址</button>
		</view>
	</view>
</template>
<script>
	import qs from 'qs'
	import {
		Storage
	} from "@/static/libs/utils"
	export default {
		data() {
			return {
				addressList: [],
				token: '',
				type: 0, //判断是否返回
			}
		},
		onLoad(e) {
			this.type = e.type || 0;
			//获取token
			this.token = Storage.get('token');
			//获取收获地址
			this.$request.post(
					`/waimai_go/app/wxapp.php?i=1&v=undefined&m=we7_wmall&c=entry&do=mobile&ctrl=wmall&ac=member&op=address&from=wxapp&state=${this.token}&sid=2&erranderId=0&agentid=0&scene=1001`
				)
				.then(res => {
					this.addressList = JSON.parse(JSON.stringify((res.data.message.message.available)));
				})
		},
		mounted() {
			
		},
		methods: {
			// 删除地址操作
			deleteBtn(index,id) {
				this.$request.get(
						`/waimai_go/app/wxapp.php?i=1&v=undefined&m=we7_wmall&c=entry&do=mobile&ctrl=wmall&ac=member&op=address&ta=del&from=wxapp&state=${this.token}&id=${id}&scene=1001`
					)
					.then(res => {
						console.log(res);
						if (res.statusCode == 200) {
							uni.showToast({
								icon: "success",
								title: "删除成功",
								duration: 2000
							})
						}
					})
				this.addressList.splice(index, 1);
			},
			edit(v) {
				console.log(v);
				var obj={};
				obj.address = v.address;
				obj.realname = v.realname;
				obj.mobile = v.mobile;
				obj.id = v.id;
				uni.navigateTo({
					url:`/pages/add/add?${qs.stringify(obj)}`
				})
			},
			goto(url) {
				uni.navigateTo({
					url
				})
			},
			chose(obj) {
				if (this.type) {
					let pages = getCurrentPages(); // 获取当前页面栈的实例，以数组形式按栈的顺序给出，第一个元素为首页，最后一个元素为当前页面。
					let nowPage = pages[pages.length - 1]; //当前页页面实例
					let prevPage = pages[pages.length - 2]; //上一页页面实例
					prevPage.$vm.otherFun(obj); // 給上一頁綁定方法otherFun,傳參object
					uni.navigateBack({
						delta: 1 // 可以不傳delta值，默認為1
					})
				}
			}
		}
	}
</script>
<style lang="scss">
	.address {
		width: 690rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 5rpx 20rpx 0rpx rgba(6, 0, 1, 0.1);
		border-radius: 14rpx;
		margin: 30rpx;
	}

	.it1 {
		margin-left: 30rpx;
		padding-top: 38rpx;
		font-size: 30rpx;
		font-weight: 500;
	}

	.it2 {
		font-size: 24rpx;
		font-family: Source Han Sans CN;
		font-weight: 400;
		color: #999999;
		margin-left: 31rpx;
		margin-top: 25rpx;
	}

	.it3 {
		margin-left: 32rpx;
		margin-top: 60rpx;
		padding-bottom: 39rpx;
		display: flex;
		flex-direction: row;
		align-items: center;
	}

	checkbox .wx-checkbox-input {
		border-color: #DCDCDC;
		border-radius: 50%;
		width: 38rpx;
		height: 39rpx;
	}

	checkbox .wx-checkbox-input.wx-checkbox-input-checked {
		color: #FFFFFF;
		background: #00C657;
	}

	.btn {
		display: flex;
		flex-direction: row;
		align-items: center;

		image {
			width: 30rpx;
			height: 32rpx;
		}

		text {
			font-size: 26rpx;
			font-family: Source Han Sans CN;
			font-weight: 400;
			color: #999999;
			margin-left: 10rpx;
		}
	}

	.footer button {
		width: 690rpx;
		height: 90rpx;
		line-height: 90rpx;
		background: #00C657;
		border-radius: 45rpx;
		margin: 0 auto;
		font-size: 28rpx;
		font-family: Microsoft YaHei;
		font-weight: 400;
		color: #FFFFFF;
	}
</style>
